#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:zhuyan
@file: dataframe_demo.py
@time: 2018/09/20
"""
from data_structure import *


class DataFrameDemo:

    def __init__(self):
        """
                使用datetime索引和标记创建DataFrame
                :return:
                """
        dates = pd.date_range('20180920', periods=7)
        print dates
        """
        randn:产生7行4列随机数
        index: 数据列索引
        columns:列的表头
        """
        self.df = pd.DataFrame(np.random.randn(7, 4), index=dates, columns=list('ABCD'))
        print self.df
        print u'=' * 10+u"初始化完成"
        self.dates = dates

    def query__(self):
        # 查看数据
        print self.df.tail(2)

        print u'====显示索引'
        print self.df.index

        print u'====显示列'
        print self.df.columns

        print u'====显示值'
        print self.df.values

        print u'====转置'
        print self.df.T

    def sort__(self):
        print self.df.sort_values(by='B')
        pass

    def statistics__(self):
        print self.df.describe()

    def select__(self):
        # 选择一列
        print self.df['A']
        # 切片，按行
        print self.df[1:2]
        # 指定选择日期
        print self.df['20180921':'20180924']

        # 按标签选择
        print self.df.loc[self.dates[0]]

        # 通过标签选择多轴
        print self.df.loc[:, ['A', 'B']]

    @staticmethod
    def new_dateframe2__():
        """
        类似系列的对象的字典来创建DataFrame
        :return:
        """

        df = pd.DataFrame({
            'A': 1.,
            'B': pd.Timestamp('20180920'),
            'C': np.array([3] * 4, dtype='int32'),
            'E': pd.Categorical(["test", "a", "train", 'test']),
            'F': pd.Series(1, index=list(range(4)))
        })
        print df


if __name__ == '__main__':
    # SeriesDemo.new_series__()
    demo = DataFrameDemo()
    # demo.query__()
    # demo.statistics__()
    # demo.sort__()
    demo.select__()

